//
//  Vector.m
//  Coffee
//
//  Created by sabhanayagam b on 5/2/11.
//  Copyright 2011 Cognizant. All rights reserved.
//

#import "Vector.h"


@implementation Vector

@synthesize vx,vy,rx,ry,lx,ly,dx,dy,length;
@synthesize p0,p1;

-(void)implementation
{
	//vector Length
	self.length = sqrt(vx*vx + vy*vy);
	
	//Unit Vector Components
	if(length !=0)
	{
		dx = vx/length;
		dy = vy/length;
	}
	//Right hand and left hand normals can be calculated:
	self.rx = -vy;
	self.ry = vx; 
	self.lx = vy;
	self.ly = -vx; 
}


-(id) initwithComponentsvx:(float) xcomponent vy:(float) ycomponent
{
	if ((self = [super init])) {
		//Assin vector components
		self.vx = xcomponent;
		self.vy = ycomponent;
		[self implementation];
	}
	
    return self;
}

-(id) initwithPointA:(CGPoint) point0 PointB:(CGPoint)point1
{
	if ((self = [super init])) {
		//Set Points
		self.p0 = point0;
		self.p1 = point1;
		[self initwithComponentsvx:(p1.x - p0.x) vy:(p1.y - p0.y)];
	}
	return self;
}

//Static Methods

+(float) dotProductVectorA:(Vector*)vec1 VectorB:(Vector*)vec2
{
	return (vec1.vx*vec2.vx + vec1.vy*vec2.vy);
}

+(float) dotProductForIntersectionVectorA:(Vector*)vec1 VectorB:(Vector*)vec2
{
	/*
	 ------v.rx = -v.vy;
	 ------v.ry = v.vx;
	 so when we replace the v1 with its normal we get:
	 -------pp = -v1.vy*v2.vx + v1.vx*v2.vy;
	 
	*/

	return (-vec1.vy*vec2.vx + vec1.vx*vec2.vy);
}


+(Vector*) projectionVectorA:(Vector*)vec1 VectorB:(Vector*)vec2
{
	float projdp = [Vector dotProductVectorA:vec1 VectorB:vec2];

	float projvx = projdp * vec2.dx;
	float projvy = projdp * vec2.dy;

	Vector *projection = [[[Vector alloc] initwithComponentsvx:projvx vy:projvy] autorelease];
	
	return projection;
	
}
+(BOOL) isParallelVectorA:(Vector*)v1 VectorB:(Vector*)v2
{
	return ((v1.dx==v2.dx && v1.dy==v2.dy) || (v1.dx==-v2.dx && v1.dy==-v2.dy));
}
-(void) dealloc
{	
	
	// don't forget to call "super dealloc"
	[super dealloc];
}
@end
